package com.ruoyi.web.controller.fantasy;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.fantasy.domain.FantasyNpc;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.web.controller.system.cloud.CloudStorageService;
import com.ruoyi.web.controller.system.cloud.OSSFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fantasy.service.IFantasyNpcService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * npc 信息操作处理
 * 
 * @author wanghui
 * @date 2019-08-02
 */
@Slf4j
@Controller
@RequestMapping("/fantasy/fantasyNpc")
public class FantasyNpcController extends BaseController
{
    private String prefix = "fantasy/fantasyNpc";
	
	@Autowired
	private IFantasyNpcService fantasyNpcService;

	@Autowired
	private ISysOssService sysOssService;

	@RequiresPermissions("fantasy:fantasyNpc:view")
	@GetMapping()
	public String fantasyNpc()
	{
	    return prefix + "/fantasyNpc";
	}
	
	/**
	 * 查询npc列表
	 */
	@RequiresPermissions("fantasy:fantasyNpc:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyNpc fantasyNpc)
	{
		startPage();
        List<FantasyNpc> list = fantasyNpcService.selectFantasyNpcList(fantasyNpc);
		fantasyNpcService.transforData(list);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出npc列表
	 */
	@RequiresPermissions("fantasy:fantasyNpc:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyNpc fantasyNpc)
    {
    	List<FantasyNpc> list = fantasyNpcService.selectFantasyNpcList(fantasyNpc);
        ExcelUtil<FantasyNpc> util = new ExcelUtil<FantasyNpc>(FantasyNpc.class);
        return util.exportExcel(list, "fantasyNpc");
    }
	
	/**
	 * 新增npc
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存npc
	 */
	@RequiresPermissions("fantasy:fantasyNpc:add")
	@Log(title = "npc", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyNpc fantasyNpc, @RequestParam("npc") MultipartFile file)
	{
		try {
			if (!file.isEmpty()) {
				String npc = FileUploadUtils.upload(Global.getNpcPath(), file);
				String relativePath = npc.substring(npc.indexOf("/", 1) + 1);
				fantasyNpc.setNpcFace(relativePath);
				String suffix = relativePath.substring(relativePath.lastIndexOf("."));
				CloudStorageService storage = OSSFactory.build();
				String url = storage.upload(file.getBytes(), relativePath);
				// 保存文件信息
				SysOss ossEntity = new SysOss();
				ossEntity.setUrl(url);
				ossEntity.setFileSuffix(suffix);
				ossEntity.setCreateBy(ShiroUtils.getLoginName());
				ossEntity.setFileName(relativePath);
				ossEntity.setCreateTime(new Date());
				ossEntity.setService(storage.getService());
				sysOssService.save(ossEntity);
			}// 取身份信息
			SysUser user = ShiroUtils.getSysUser();
			fantasyNpc.setNpcId(String.valueOf(IdWorker.nextId()));
			fantasyNpc.setCreateBy(user.getUserId().toString());
			fantasyNpc.setCreateTime(new Date());
			return toAjax(fantasyNpcService.insertFantasyNpc(fantasyNpc));
		} catch (IOException e) {
			log.error("新增保存npc失败！", e);
			return error(e.getMessage());
		}
	}

	/**
	 * 修改npc
	 */
	@GetMapping("/edit/{npcId}")
	public String edit(@PathVariable("npcId") String npcId, ModelMap mmap)
	{
		FantasyNpc fantasyNpc = fantasyNpcService.selectFantasyNpcById(npcId);
		mmap.put("fantasyNpc", fantasyNpc);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存npc
	 */
	@RequiresPermissions("fantasy:fantasyNpc:edit")
	@Log(title = "npc", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyNpc fantasyNpc, @RequestParam("npc") MultipartFile file)
	{
		try {
			if (!file.isEmpty()) {
				String npc = FileUploadUtils.upload(Global.getNpcPath(), file);
				String relativePath = npc.substring(npc.indexOf("/", 1) + 1);
				fantasyNpc.setNpcFace(relativePath);
				String suffix = relativePath.substring(relativePath.lastIndexOf("."));
				CloudStorageService storage = OSSFactory.build();
				String url = storage.upload(file.getBytes(), relativePath);
				// 保存文件信息
				SysOss ossEntity = new SysOss();
				ossEntity.setUrl(url);
				ossEntity.setFileSuffix(suffix);
				ossEntity.setCreateBy(ShiroUtils.getLoginName());
				ossEntity.setFileName(relativePath);
				ossEntity.setCreateTime(new Date());
				ossEntity.setService(storage.getService());
				sysOssService.save(ossEntity);
			}
			// 取身份信息
			SysUser user = ShiroUtils.getSysUser();
			fantasyNpc.setUpdateBy(user.getUserId().toString());
			fantasyNpc.setUpdateTime(new Date());
			return toAjax(fantasyNpcService.updateFantasyNpc(fantasyNpc));
		} catch (IOException e) {
			log.error("修改保存npc失败！", e);
			return error(e.getMessage());
		}
	}
	
	/**
	 * 删除npc
	 */
	@RequiresPermissions("fantasy:fantasyNpc:remove")
	@Log(title = "npc", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyNpcService.deleteFantasyNpcByIds(ids));
	}
	
}
